using Microsoft.EntityFrameworkCore;

namespace DesktopWidgetApp.Server.Models
{
    /// <summary>
    /// 应用程序数据库上下文
    /// </summary>
    public class AppDbContext : DbContext
    {
        /// <summary>
        /// 服务器端状态集合
        /// </summary>
        public DbSet<ServerStatus> ServerStatuses { get; set; }
        
        /// <summary>
        /// 用户集合
        /// </summary>
        public DbSet<User> Users { get; set; }
        
        /// <summary>
        /// 团队集合
        /// </summary>
        public DbSet<Team> Teams { get; set; }
        
        /// <summary>
        /// 团队成员集合
        /// </summary>
        public DbSet<TeamMember> TeamMembers { get; set; }

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="options">数据库上下文选项</param>
        public AppDbContext(DbContextOptions<AppDbContext> options) : base(options)
        { }

        /// <summary>
        /// 配置模型
        /// </summary>
        /// <param name="modelBuilder">模型构建器</param>
        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            // 配置ServerStatus实体
            modelBuilder.Entity<ServerStatus>()
                .ToTable("Statuses")
                .HasKey(s => s.Id);

            modelBuilder.Entity<ServerStatus>()
                .Property(s => s.UserId)
                .IsRequired();

            modelBuilder.Entity<ServerStatus>()
                .Property(s => s.StatusType)
                .IsRequired();

            modelBuilder.Entity<ServerStatus>()
                .Property(s => s.StartTime)
                .IsRequired();

            modelBuilder.Entity<ServerStatus>()
                .Property(s => s.LastUpdated)
                .IsRequired();
                
            // 配置User实体
            modelBuilder.Entity<User>()
                .ToTable("Users")
                .HasKey(u => u.Id);
                
            modelBuilder.Entity<User>()
                .Property(u => u.Username)
                .IsRequired()
                .HasMaxLength(50);
                
            modelBuilder.Entity<User>()
                .Property(u => u.Email)
                .IsRequired()
                .HasMaxLength(100);
                
            modelBuilder.Entity<User>()
                .Property(u => u.PasswordHash)
                .IsRequired();
                
            modelBuilder.Entity<User>()
                .Property(u => u.PasswordSalt)
                .IsRequired();
                
            modelBuilder.Entity<User>()
                .Property(u => u.RegisteredAt)
                .IsRequired();
                
            modelBuilder.Entity<User>()
                .Property(u => u.Role)
                .IsRequired();
                
            // 添加唯一约束
            modelBuilder.Entity<User>()
                .HasIndex(u => u.Username)
                .IsUnique();
                
            modelBuilder.Entity<User>()
                .HasIndex(u => u.Email)
                .IsUnique();
                
            // 配置关系
            modelBuilder.Entity<User>()
                .HasMany(u => u.Statuses)
                .WithOne()
                .HasForeignKey(s => s.UserId);
                
            // 配置Team实体
            modelBuilder.Entity<Team>()
                .ToTable("Teams")
                .HasKey(t => t.Id);
                
            modelBuilder.Entity<Team>()
                .Property(t => t.Name)
                .IsRequired()
                .HasMaxLength(100);
                
            modelBuilder.Entity<Team>()
                .Property(t => t.CreatedAt)
                .IsRequired();
                
            modelBuilder.Entity<Team>()
                .Property(t => t.CreatorId)
                .IsRequired();
                
            // 配置TeamMember实体
            modelBuilder.Entity<TeamMember>()
                .ToTable("TeamMembers")
                .HasKey(tm => new { tm.TeamId, tm.UserId });
                
            modelBuilder.Entity<TeamMember>()
                .Property(tm => tm.Role)
                .IsRequired()
                .HasMaxLength(50);
                
            modelBuilder.Entity<TeamMember>()
                .Property(tm => tm.JoinedAt)
                .IsRequired();
                
            // 配置关系
            modelBuilder.Entity<Team>()
                .HasOne(t => t.Creator)
                .WithMany()
                .HasForeignKey(t => t.CreatorId);
                
            modelBuilder.Entity<Team>()
                .HasMany(t => t.Members)
                .WithOne(tm => tm.Team)
                .HasForeignKey(tm => tm.TeamId);
                
            modelBuilder.Entity<TeamMember>()
                .HasOne(tm => tm.User)
                .WithMany()
                .HasForeignKey(tm => tm.UserId);
        }
    }
}